<?php

namespace App\Job\Delay;

use App\Actions\PaycallAction;
use App\Dao\PayCallDao;
use App\Utils\LoggerUtil;
use App\Utils\RedisClient;
use Hyperf\AsyncQueue\Job;

class RemoveBlackScreenDelayQueueJob extends Job
{
    private mixed $params;

    public function __construct($params)
    {
        // 这里最好是普通数据，不要使用携带 IO 的对象，比如 PDO 对象
        $this->params = $params;
    }


    public function handle()
    {
        // TODO: Implement handle() method.
        $body = json_decode($this->params, true);
        LoggerUtil::write("remove_black_screen_delay_queue_job", $body);
        $param = $body['param'];
        $res = $this->checkBreakRules(...$param);
        LoggerUtil::write("remove_black_screen_delay_queue_job", compact('res'));
    }

    /**
     * 主播违规黑屏解除
     * @param $call_id
     * @param $uid
     * @return bool
     */
    public function checkBreakRules($call_id, $uid)
    {
        $payCall = PayCallDao::getInstance()->getCallCache($call_id, true);
        if ($payCall['status'] == 1) {
            return true;
        }

        $redis = RedisClient::getInstance();
        $call_break_rules = $redis->get("call_break_rules:{$call_id}_{$uid}");
        LoggerUtil::write("remove_black_screen_delay_queue_job", compact("payCall", 'call_break_rules'));

        if (!$call_break_rules) {
            if ($uid == $payCall['user_id']) {
                //取消黑屏
                (new PaycallAction())->cancelBlackScreenMsg($payCall['user_id'], $payCall['anchor_id'], $call_id, $payCall['type']);
            } else {
                //取消黑屏
                (new PaycallAction())->cancelBlackScreenMsg($payCall['anchor_id'], $payCall['user_id'], $call_id, $payCall['type']);
            }
        }
        return true;
    }


}